package top.malaoshi.data;

import com.fasterxml.jackson.databind.ObjectMapper;
import top.malaoshi.util.io.TextFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class DataFile {

    /**
     * 将DataTable写入到json
     * @param dataTable
     */
    public static void writeJson(DataTable dataTable, String path) throws IOException {

        Map<String,Object> tableMap = buildTableJson(dataTable);
        write(tableMap,path);
    }

    /**
     * 组织 DataTable json
     * @param dataTable
     * @return
     */
    private static Map<String,Object> buildTableJson( DataTable dataTable){
        Map<String,Object > tableMap = new LinkedHashMap<>();
        tableMap.put("TableName",dataTable.getName());

        // 总共列数
        int countOfColumns = dataTable.columns().count();
        // 组装列
        List<Map<String,Object>> columnList = new ArrayList<>();
        for(int j = 0;j<countOfColumns;j++) {
            DataColumn dataColumn = dataTable.columns().get(j);
            Map<String,Object > columnMap = new LinkedHashMap<>();
            columnMap.put("ColumnName", dataColumn.getName());
            columnMap.put("DataType", dataColumn.getType());
            columnList.add(columnMap);
        }

        tableMap.put("Columns",columnList);

        // 组装数据
        List<Map<String,Object>> rowList = new ArrayList<>();
        for(int j = 0,jcount = dataTable.rows().count();j<jcount;j++) {
            DataRow dataRow = dataTable.rows().get(j);

            // 组装一行数据
            Map<String, Object> datasOfRow = new LinkedHashMap<>();
            for(int k=0;k<countOfColumns;k++) {
                datasOfRow.put(dataTable.columns().getNames()[k], dataRow.getValue(k));
            }
            rowList.add(datasOfRow);
        }
        tableMap.put("Rows",rowList);
        return tableMap;
    }
    /**
     * 将 DataSet 写入到json
     * @param dataSet
     */
    public static void writeJson(DataSet dataSet,String path) throws IOException {

        Map<String,Object> dataSetMap = new LinkedHashMap<>();

        dataSetMap.put("DataSetName", dataSet.getName());

        // 组装表
        List<Map<String,Object>> tableList = new ArrayList<>();
        for(int i = 0,count = dataSet.tables().count();i<count;i++) {
            DataTable dataTable = dataSet.tables().get(i);
            Map<String,Object> tableMap = buildTableJson(dataTable);
            tableList.add(tableMap);
        }

        dataSetMap.put("Tables", tableList);

        write(dataSetMap,path);
    }

    private static void write(Object obj,String path) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(obj);
        TextFile.writeAll(path,"utf-8",json);
    }
    /**
     * 读取table 格式的json文件
     * @param path
     */
    public DataTable readDataTableJson(String path){

        return null;
    }

    /**
     * 读取 dataset 格式的json文件
     * @param path
     * @return
     */
    public DataSet readDataSetJson(String path){
        return null;
    }

}
